package cn.iocoder.yudao.module.shop.controller.admin.itemtimesrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.itemtimesrecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemtimesrecord.ShopItemTimesRecordDO;
import cn.iocoder.yudao.module.shop.convert.itemtimesrecord.ShopItemTimesRecordConvert;
import cn.iocoder.yudao.module.shop.service.itemtimesrecord.ShopItemTimesRecordService;

@Tag(name = "管理后台 - 项目次数变化记录")
@RestController
@RequestMapping("/shop/item-times-record")
@Validated
public class ShopItemTimesRecordController {

    @Resource
    private ShopItemTimesRecordService itemTimesRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建项目次数变化记录")
    @PreAuthorize("@ss.hasPermission('shop:item-times-record:create')")
    public CommonResult<Long> createItemTimesRecord(@Valid @RequestBody ShopItemTimesRecordCreateReqVO createReqVO) {
        return success(itemTimesRecordService.createItemTimesRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目次数变化记录")
    @PreAuthorize("@ss.hasPermission('shop:item-times-record:update')")
    public CommonResult<Boolean> updateItemTimesRecord(@Valid @RequestBody ShopItemTimesRecordUpdateReqVO updateReqVO) {
        itemTimesRecordService.updateItemTimesRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目次数变化记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:item-times-record:delete')")
    public CommonResult<Boolean> deleteItemTimesRecord(@RequestParam("id") Long id) {
        itemTimesRecordService.deleteItemTimesRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目次数变化记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:item-times-record:query')")
    public CommonResult<ShopItemTimesRecordRespVO> getItemTimesRecord(@RequestParam("id") Long id) {
        ShopItemTimesRecordDO itemTimesRecord = itemTimesRecordService.getItemTimesRecord(id);
        return success(ShopItemTimesRecordConvert.INSTANCE.convert(itemTimesRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得项目次数变化记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:item-times-record:query')")
    public CommonResult<List<ShopItemTimesRecordRespVO>> getItemTimesRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopItemTimesRecordDO> list = itemTimesRecordService.getItemTimesRecordList(ids);
        return success(ShopItemTimesRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目次数变化记录分页")
    @PreAuthorize("@ss.hasPermission('shop:item-times-record:query')")
    public CommonResult<PageResult<ShopItemTimesRecordRespVO>> getItemTimesRecordPage(@Valid ShopItemTimesRecordPageReqVO pageVO) {
        PageResult<ShopItemTimesRecordDO> pageResult = itemTimesRecordService.getItemTimesRecordPage(pageVO);
        return success(ShopItemTimesRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目次数变化记录 Excel")
    @PreAuthorize("@ss.hasPermission('shop:item-times-record:export')")
    @OperateLog(type = EXPORT)
    public void exportItemTimesRecordExcel(@Valid ShopItemTimesRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopItemTimesRecordDO> list = itemTimesRecordService.getItemTimesRecordList(exportReqVO);
        // 导出 Excel
        List<ShopItemTimesRecordExcelVO> datas = ShopItemTimesRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目次数变化记录.xls", "数据", ShopItemTimesRecordExcelVO.class, datas);
    }

}
